package org.jeecg.modules.ncmeapis.processTest.business.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import ncme.digital.hszy.ep.db.entity.ProcessInstance;
import ncme.digital.hszy.ep.enums.ProcessState;
import ncme.digital.hszy.ep.interfaces.NodeHandler;
import org.jeecg.modules.ncmeapis.processTest.business.entity.HolidayForm;
import org.jeecg.modules.ncmeapis.processTest.processDefinition.holiday.HolidayProcess;
import org.jeecg.modules.ncmeapis.processTest.processDefinition.holiday.nodes.ApplyHolidayNode;
import org.jeecg.modules.ncmeapis.processTest.processDefinition.overtime.OvertimeProcess;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.List;

@Api(tags = "流程并发测试")
//@BusinessApi
//@RestController
@RequestMapping("/demo/concurrency")
@Slf4j
public class ProcessConcurrencyController {

    @Autowired
    private HolidayProcess holidayProcess;
    @Autowired
    private OvertimeProcess overtimeProcess;

    @ApiOperation(value = "开启两个流程并下一步")
    @GetMapping(value = "/startTwoProcess")
    public Object startTwoProcess() throws Exception {
        String operator = "孙";
        Integer processId = overtimeProcess.start(operator);
        Integer processId2 = holidayProcess.start(operator);
        overtimeProcess.push(processId, operator);
        holidayProcess.push(processId2, operator, ApplyHolidayNode.class, new NodeHandler<ApplyHolidayNode>() {
            @Override
            public boolean Handle(ApplyHolidayNode processNode) throws Exception {
                HolidayForm holidayForm = new HolidayForm();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                holidayForm.setBeginDate(sdf.parse("2023-11-11"));
                holidayForm.setEndDate(sdf.parse("2023-11-14"));
                holidayForm.setType("病假");
                processNode.setHolidayForm(holidayForm);
                return true;
            }
        });
        return "ok";
    }

    @ApiOperation(value = "查看所有流程")
    @GetMapping(value = "/instanceList")
    public Object instanceList() throws Exception {
        List<ProcessInstance> allProcessInstances = holidayProcess.getAllProcessInstances(ProcessState.END);
        return allProcessInstances;
    }

}
